package com.xdtech.cc.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.xdtech.sys.aspect.SystemControllerLog;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.cc.service.AccountService;
import com.xdtech.cc.vo.AccountItem;
import com.xdtech.web.controller.BaseController;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2016-05-29 21:53:30
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class AccountController extends BaseController{
	private Log log = LogFactory.getLog(AccountController.class);
	@Autowired
	private AccountService accountService;
	@RequestMapping(value="/account.do",params = "account")
	public ModelAndView skipAccount() {
		return new ModelAndView("cc/account/account_ftl");
	}
	
	@RequestMapping(value="/account.do",params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(AccountItem item,Pagination pg) {
		Map<String, Object> results =  accountService.loadPageDataByConditions(pg,item,"findAccountByCondition");
		return results;
	}
	
	@RequestMapping(value="/account.do",params = "editAccount")
	@SystemControllerLog(description = "编辑")
	public ModelAndView editAccount(HttpServletRequest request,Long accountId) {
		if (accountId!=null) {
			request.setAttribute("accountItem", accountService.loadAccountItem(accountId));
		}
		return new ModelAndView("cc/account/editAccount_ftl");
	}
	
	@RequestMapping(value="/account.do",params = "saveAccount")
	@ResponseBody
	@SystemControllerLog(description = "保存")
	public ResultMessage saveAccount(AccountItem item) {
		ResultMessage r = new ResultMessage();
		if (accountService.saveOrUpdateAccount(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/account.do",params = "deleteAccountItems")
	@ResponseBody
	@SystemControllerLog(description = "删除")
	public ResultMessage deleteAccountItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> accountIds = new ArrayList<Long>();
			for (String id : tempIds) {
				accountIds.add(Long.valueOf(id));
			}
			accountService.deleteAccountInfo(accountIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}

}
